// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Generated by the Codegen C++ plugin.
// If you make any local changes, they will be lost.
// source: google/cloud/secretmanager/v1/service.proto

#include "google/cloud/secretmanager/secret_manager_connection.h"
#include "google/cloud/secretmanager/internal/secret_manager_option_defaults.h"
#include "google/cloud/secretmanager/internal/secret_manager_stub_factory.h"
#include "google/cloud/secretmanager/secret_manager_options.h"
#include "google/cloud/background_threads.h"
#include "google/cloud/common_options.h"
#include "google/cloud/grpc_options.h"
#include "google/cloud/internal/pagination_range.h"
#include "google/cloud/internal/retry_loop.h"
#include <memory>

namespace google {
namespace cloud {
namespace secretmanager {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

SecretManagerServiceConnection::~SecretManagerServiceConnection() = default;

StreamRange<google::cloud::secretmanager::v1::Secret>
SecretManagerServiceConnection::ListSecrets(
    google::cloud::secretmanager::v1::ListSecretsRequest request) {
  return google::cloud::internal::MakePaginationRange<
      StreamRange<google::cloud::secretmanager::v1::Secret>>(
      std::move(request),
      [](google::cloud::secretmanager::v1::ListSecretsRequest const&) {
        return StatusOr<
            google::cloud::secretmanager::v1::ListSecretsResponse>{};
      },
      [](google::cloud::secretmanager::v1::ListSecretsResponse const&) {
        return std::vector<google::cloud::secretmanager::v1::Secret>();
      });
}

StatusOr<google::cloud::secretmanager::v1::Secret>
SecretManagerServiceConnection::CreateSecret(
    google::cloud::secretmanager::v1::CreateSecretRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::cloud::secretmanager::v1::SecretVersion>
SecretManagerServiceConnection::AddSecretVersion(
    google::cloud::secretmanager::v1::AddSecretVersionRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::cloud::secretmanager::v1::Secret>
SecretManagerServiceConnection::GetSecret(
    google::cloud::secretmanager::v1::GetSecretRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::cloud::secretmanager::v1::Secret>
SecretManagerServiceConnection::UpdateSecret(
    google::cloud::secretmanager::v1::UpdateSecretRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

Status SecretManagerServiceConnection::DeleteSecret(
    google::cloud::secretmanager::v1::DeleteSecretRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StreamRange<google::cloud::secretmanager::v1::SecretVersion>
SecretManagerServiceConnection::ListSecretVersions(
    google::cloud::secretmanager::v1::ListSecretVersionsRequest request) {
  return google::cloud::internal::MakePaginationRange<
      StreamRange<google::cloud::secretmanager::v1::SecretVersion>>(
      std::move(request),
      [](google::cloud::secretmanager::v1::ListSecretVersionsRequest const&) {
        return StatusOr<
            google::cloud::secretmanager::v1::ListSecretVersionsResponse>{};
      },
      [](google::cloud::secretmanager::v1::ListSecretVersionsResponse const&) {
        return std::vector<google::cloud::secretmanager::v1::SecretVersion>();
      });
}

StatusOr<google::cloud::secretmanager::v1::SecretVersion>
SecretManagerServiceConnection::GetSecretVersion(
    google::cloud::secretmanager::v1::GetSecretVersionRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::cloud::secretmanager::v1::AccessSecretVersionResponse>
SecretManagerServiceConnection::AccessSecretVersion(
    google::cloud::secretmanager::v1::AccessSecretVersionRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::cloud::secretmanager::v1::SecretVersion>
SecretManagerServiceConnection::DisableSecretVersion(
    google::cloud::secretmanager::v1::DisableSecretVersionRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::cloud::secretmanager::v1::SecretVersion>
SecretManagerServiceConnection::EnableSecretVersion(
    google::cloud::secretmanager::v1::EnableSecretVersionRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::cloud::secretmanager::v1::SecretVersion>
SecretManagerServiceConnection::DestroySecretVersion(
    google::cloud::secretmanager::v1::DestroySecretVersionRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::iam::v1::Policy> SecretManagerServiceConnection::SetIamPolicy(
    google::iam::v1::SetIamPolicyRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::iam::v1::Policy> SecretManagerServiceConnection::GetIamPolicy(
    google::iam::v1::GetIamPolicyRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

StatusOr<google::iam::v1::TestIamPermissionsResponse>
SecretManagerServiceConnection::TestIamPermissions(
    google::iam::v1::TestIamPermissionsRequest const&) {
  return Status(StatusCode::kUnimplemented, "not implemented");
}

namespace {
class SecretManagerServiceConnectionImpl
    : public SecretManagerServiceConnection {
 public:
  SecretManagerServiceConnectionImpl(
      std::unique_ptr<google::cloud::BackgroundThreads> background,
      std::shared_ptr<secretmanager_internal::SecretManagerServiceStub> stub,
      Options const& options)
      : background_(std::move(background)),
        stub_(std::move(stub)),
        retry_policy_prototype_(
            options.get<SecretManagerServiceRetryPolicyOption>()->clone()),
        backoff_policy_prototype_(
            options.get<SecretManagerServiceBackoffPolicyOption>()->clone()),
        idempotency_policy_(
            options
                .get<SecretManagerServiceConnectionIdempotencyPolicyOption>()
                ->clone()) {}

  ~SecretManagerServiceConnectionImpl() override = default;

  StreamRange<google::cloud::secretmanager::v1::Secret> ListSecrets(
      google::cloud::secretmanager::v1::ListSecretsRequest request) override {
    request.clear_page_token();
    auto stub = stub_;
    auto retry = std::shared_ptr<SecretManagerServiceRetryPolicy const>(
        retry_policy_prototype_->clone());
    auto backoff = std::shared_ptr<BackoffPolicy const>(
        backoff_policy_prototype_->clone());
    auto idempotency = idempotency_policy_->ListSecrets(request);
    char const* function_name = __func__;
    return google::cloud::internal::MakePaginationRange<
        StreamRange<google::cloud::secretmanager::v1::Secret>>(
        std::move(request),
        [stub, retry, backoff, idempotency, function_name](
            google::cloud::secretmanager::v1::ListSecretsRequest const& r) {
          return google::cloud::internal::RetryLoop(
              retry->clone(), backoff->clone(), idempotency,
              [stub](grpc::ClientContext& context,
                     google::cloud::secretmanager::v1::ListSecretsRequest const&
                         request) {
                return stub->ListSecrets(context, request);
              },
              r, function_name);
        },
        [](google::cloud::secretmanager::v1::ListSecretsResponse r) {
          std::vector<google::cloud::secretmanager::v1::Secret> result(
              r.secrets().size());
          auto& messages = *r.mutable_secrets();
          std::move(messages.begin(), messages.end(), result.begin());
          return result;
        });
  }

  StatusOr<google::cloud::secretmanager::v1::Secret> CreateSecret(
      google::cloud::secretmanager::v1::CreateSecretRequest const& request)
      override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->CreateSecret(request),
        [this](grpc::ClientContext& context,
               google::cloud::secretmanager::v1::CreateSecretRequest const&
                   request) { return stub_->CreateSecret(context, request); },
        request, __func__);
  }

  StatusOr<google::cloud::secretmanager::v1::SecretVersion> AddSecretVersion(
      google::cloud::secretmanager::v1::AddSecretVersionRequest const& request)
      override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->AddSecretVersion(request),
        [this](grpc::ClientContext& context,
               google::cloud::secretmanager::v1::AddSecretVersionRequest const&
                   request) {
          return stub_->AddSecretVersion(context, request);
        },
        request, __func__);
  }

  StatusOr<google::cloud::secretmanager::v1::Secret> GetSecret(
      google::cloud::secretmanager::v1::GetSecretRequest const& request)
      override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->GetSecret(request),
        [this](
            grpc::ClientContext& context,
            google::cloud::secretmanager::v1::GetSecretRequest const& request) {
          return stub_->GetSecret(context, request);
        },
        request, __func__);
  }

  StatusOr<google::cloud::secretmanager::v1::Secret> UpdateSecret(
      google::cloud::secretmanager::v1::UpdateSecretRequest const& request)
      override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->UpdateSecret(request),
        [this](grpc::ClientContext& context,
               google::cloud::secretmanager::v1::UpdateSecretRequest const&
                   request) { return stub_->UpdateSecret(context, request); },
        request, __func__);
  }

  Status DeleteSecret(
      google::cloud::secretmanager::v1::DeleteSecretRequest const& request)
      override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->DeleteSecret(request),
        [this](grpc::ClientContext& context,
               google::cloud::secretmanager::v1::DeleteSecretRequest const&
                   request) { return stub_->DeleteSecret(context, request); },
        request, __func__);
  }

  StreamRange<google::cloud::secretmanager::v1::SecretVersion>
  ListSecretVersions(google::cloud::secretmanager::v1::ListSecretVersionsRequest
                         request) override {
    request.clear_page_token();
    auto stub = stub_;
    auto retry = std::shared_ptr<SecretManagerServiceRetryPolicy const>(
        retry_policy_prototype_->clone());
    auto backoff = std::shared_ptr<BackoffPolicy const>(
        backoff_policy_prototype_->clone());
    auto idempotency = idempotency_policy_->ListSecretVersions(request);
    char const* function_name = __func__;
    return google::cloud::internal::MakePaginationRange<
        StreamRange<google::cloud::secretmanager::v1::SecretVersion>>(
        std::move(request),
        [stub, retry, backoff, idempotency, function_name](
            google::cloud::secretmanager::v1::ListSecretVersionsRequest const&
                r) {
          return google::cloud::internal::RetryLoop(
              retry->clone(), backoff->clone(), idempotency,
              [stub](grpc::ClientContext& context,
                     google::cloud::secretmanager::v1::
                         ListSecretVersionsRequest const& request) {
                return stub->ListSecretVersions(context, request);
              },
              r, function_name);
        },
        [](google::cloud::secretmanager::v1::ListSecretVersionsResponse r) {
          std::vector<google::cloud::secretmanager::v1::SecretVersion> result(
              r.versions().size());
          auto& messages = *r.mutable_versions();
          std::move(messages.begin(), messages.end(), result.begin());
          return result;
        });
  }

  StatusOr<google::cloud::secretmanager::v1::SecretVersion> GetSecretVersion(
      google::cloud::secretmanager::v1::GetSecretVersionRequest const& request)
      override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->GetSecretVersion(request),
        [this](grpc::ClientContext& context,
               google::cloud::secretmanager::v1::GetSecretVersionRequest const&
                   request) {
          return stub_->GetSecretVersion(context, request);
        },
        request, __func__);
  }

  StatusOr<google::cloud::secretmanager::v1::AccessSecretVersionResponse>
  AccessSecretVersion(
      google::cloud::secretmanager::v1::AccessSecretVersionRequest const&
          request) override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->AccessSecretVersion(request),
        [this](
            grpc::ClientContext& context,
            google::cloud::secretmanager::v1::AccessSecretVersionRequest const&
                request) {
          return stub_->AccessSecretVersion(context, request);
        },
        request, __func__);
  }

  StatusOr<google::cloud::secretmanager::v1::SecretVersion>
  DisableSecretVersion(
      google::cloud::secretmanager::v1::DisableSecretVersionRequest const&
          request) override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->DisableSecretVersion(request),
        [this](
            grpc::ClientContext& context,
            google::cloud::secretmanager::v1::DisableSecretVersionRequest const&
                request) {
          return stub_->DisableSecretVersion(context, request);
        },
        request, __func__);
  }

  StatusOr<google::cloud::secretmanager::v1::SecretVersion> EnableSecretVersion(
      google::cloud::secretmanager::v1::EnableSecretVersionRequest const&
          request) override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->EnableSecretVersion(request),
        [this](
            grpc::ClientContext& context,
            google::cloud::secretmanager::v1::EnableSecretVersionRequest const&
                request) {
          return stub_->EnableSecretVersion(context, request);
        },
        request, __func__);
  }

  StatusOr<google::cloud::secretmanager::v1::SecretVersion>
  DestroySecretVersion(
      google::cloud::secretmanager::v1::DestroySecretVersionRequest const&
          request) override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->DestroySecretVersion(request),
        [this](
            grpc::ClientContext& context,
            google::cloud::secretmanager::v1::DestroySecretVersionRequest const&
                request) {
          return stub_->DestroySecretVersion(context, request);
        },
        request, __func__);
  }

  StatusOr<google::iam::v1::Policy> SetIamPolicy(
      google::iam::v1::SetIamPolicyRequest const& request) override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->SetIamPolicy(request),
        [this](grpc::ClientContext& context,
               google::iam::v1::SetIamPolicyRequest const& request) {
          return stub_->SetIamPolicy(context, request);
        },
        request, __func__);
  }

  StatusOr<google::iam::v1::Policy> GetIamPolicy(
      google::iam::v1::GetIamPolicyRequest const& request) override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->GetIamPolicy(request),
        [this](grpc::ClientContext& context,
               google::iam::v1::GetIamPolicyRequest const& request) {
          return stub_->GetIamPolicy(context, request);
        },
        request, __func__);
  }

  StatusOr<google::iam::v1::TestIamPermissionsResponse> TestIamPermissions(
      google::iam::v1::TestIamPermissionsRequest const& request) override {
    return google::cloud::internal::RetryLoop(
        retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
        idempotency_policy_->TestIamPermissions(request),
        [this](grpc::ClientContext& context,
               google::iam::v1::TestIamPermissionsRequest const& request) {
          return stub_->TestIamPermissions(context, request);
        },
        request, __func__);
  }

 private:
  std::unique_ptr<google::cloud::BackgroundThreads> background_;
  std::shared_ptr<secretmanager_internal::SecretManagerServiceStub> stub_;
  std::unique_ptr<SecretManagerServiceRetryPolicy const>
      retry_policy_prototype_;
  std::unique_ptr<BackoffPolicy const> backoff_policy_prototype_;
  std::unique_ptr<SecretManagerServiceConnectionIdempotencyPolicy>
      idempotency_policy_;
};
}  // namespace

std::shared_ptr<SecretManagerServiceConnection>
MakeSecretManagerServiceConnection(Options options) {
  internal::CheckExpectedOptions<CommonOptionList, GrpcOptionList,
                                 SecretManagerServicePolicyOptionList>(
      options, __func__);
  options = secretmanager_internal::SecretManagerServiceDefaultOptions(
      std::move(options));
  auto background = internal::MakeBackgroundThreadsFactory(options)();
  auto stub = secretmanager_internal::CreateDefaultSecretManagerServiceStub(
      background->cq(), options);
  return std::make_shared<SecretManagerServiceConnectionImpl>(
      std::move(background), std::move(stub), options);
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
}  // namespace secretmanager
}  // namespace cloud
}  // namespace google

namespace google {
namespace cloud {
namespace secretmanager_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

std::shared_ptr<secretmanager::SecretManagerServiceConnection>
MakeSecretManagerServiceConnection(
    std::shared_ptr<SecretManagerServiceStub> stub, Options options) {
  options = SecretManagerServiceDefaultOptions(std::move(options));
  return std::make_shared<secretmanager::SecretManagerServiceConnectionImpl>(
      internal::MakeBackgroundThreadsFactory(options)(), std::move(stub),
      std::move(options));
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
}  // namespace secretmanager_internal
}  // namespace cloud
}  // namespace google
